package com.yxtbd.travel.travelvm.ext

import android.app.Activity
import com.yxtbd.travel.travelvm.model.bean.ApiResponse
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.coroutineScope
import retrofit2.HttpException

/**
 * description ： executeResponse
 *
 * author : taodq
 * date : 2020/3/31 9:15
 */

suspend fun executeResponse(
    response: ApiResponse<Any>,
    successBlock: suspend CoroutineScope.() -> Unit,
    errorBlock: suspend CoroutineScope.() -> Unit
) {
    coroutineScope {
        if (response.code == -1) errorBlock()
        else successBlock()
    }
}

fun Activity.onNetError(e: Throwable, func: (e: Throwable) -> Unit) {
    if (e is HttpException) {
        toast("网络错误")
        func(e)
    }
}

fun ApiResponse<Any>.isSuccess(): Boolean = this.code == 200